package com.inspur.cloud.controller.resource;

import com.inspur.cloud.entity.dataspace.ao.resource.DistributeAO;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.PageReqParam;
import com.inspur.cloud.entity.dataspace.common.PageResult;
import com.inspur.cloud.entity.dataspace.vo.YarnParam;
import com.inspur.cloud.entity.dataspace.vo.YarnReq;
import com.inspur.cloud.entity.dataspace.vo.YarnVO;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.service.dataspace.YarnManagerService;
import com.inspur.cloud.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.RolesAllowed;
import java.util.List;

@RestController
@RequestMapping("/dataspace/api/v1/yarn")
@Api(value = "yarn", description = "yarn资源管理")
public class YarnManagerController {
    @Autowired
    YarnManagerService yarnManagerService;

    @RequestMapping(value = "/list",method = { RequestMethod.POST })
    @ApiOperation(value = "资源列表", notes = "")
    public PageResult<YarnVO> showList(@ApiParam(value = "yarn查询对象,json格式请求")
                                         @RequestBody PageReqParam<YarnReq> yarnReq) {
        PageResult<YarnVO> result = new PageResult<>();
        if (yarnReq.getReqParam() == null) {
            result.setCode(-1);
            result.setMsg("查询参数不完整");
            return result;
        }
        if (yarnReq.getPageSize() > 50 || yarnReq.getPageSize() < 0) {
            result.setCode(-1);
            result.setMsg("页码非法");
            return result;
        }
        String resourceName=yarnReq.getReqParam().getResourceName();
        if(!StringUtil.isEmpty(resourceName)){
            yarnReq.getReqParam().setResourceName(StringUtil.specialStr(resourceName));
        }
        if (AuthorizationHelper.isAdmin()) {//预留，考虑后期不好处理的时候扩展
            result = yarnManagerService.showForAdmin(yarnReq);
        } else {
            result = yarnManagerService.showForUser(yarnReq);
        }
        return result;
    }

    @RequestMapping(value = "/queueList", method = {RequestMethod.GET})
    @ApiOperation(value = "yarn队列列表", notes = "编辑时使用")
    public PageResult<YarnVO> queueList(@ApiParam(value = "资源ID，查询第一层时传0即可")
                                        @RequestParam(value = "resourceId") Integer resourceId) {
        PageResult<YarnVO> ret = new PageResult<>();
        List<YarnVO> voList = yarnManagerService.queueList(resourceId);
        ret.setPageList(voList);
        ret.setCount(Long.valueOf(voList.size()));
        return ret;
    }

    @PostMapping("/addRes")
    @ApiOperation(value = "新建yarn资源，返回数据库里的资源id", notes = "调用时如果新建一级队列，parentId传0。即使default队列前端不可见，也需要传default队列的信息。")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> addRes(@ApiParam(value = "yarn传参")
                                          @RequestBody YarnParam yarnParam) throws Exception {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (yarnParam.getYarnInfoList() == null && yarnParam.getYarnInfoList().size() == 0) {
            return OpEntityResult.fail("队列参数不能为空");
        }
        if (yarnParam.getLevel() == null || yarnParam.getLevel() > 2) {
            return OpEntityResult.fail("层级参数必传，且最大不能超过两层");
        }
        if (yarnParam.getParentId() == null) {
            return OpEntityResult.fail("父资源ID必传");
        }
        if (yarnParam.getLevel() > 1 && yarnParam.getParentId() == 0) {
            return OpEntityResult.fail("创建子队列时父资源ID不能为0");
        }
        if (yarnParam.getType() == null) {
            yarnParam.setType(1);
        }
        result = yarnManagerService.addRes(yarnParam);
        return result;
    }

    @PostMapping("/modifyRes")
    @ApiOperation(value = "编辑yarn资源，返回数据库里的资源id", notes = "即使default队列前端不可见，也需要传default队列的信息")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> modifyRes(@ApiParam(value = "yarn传参")
                                             @RequestBody YarnParam yarnParam) throws Exception {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (yarnParam.getYarnInfoList() == null && yarnParam.getYarnInfoList().size() == 0) {
            return OpEntityResult.fail("参数不能为空");
        }
        if (yarnParam.getParentId() == null) {
            return OpEntityResult.fail("父资源ID必传");
        }
        if (yarnParam.getParentId() == 0 && !AuthorizationHelper.isAdmin()) {
            return OpEntityResult.fail("非管理员不允许编辑该级别队列");
        }
        if (yarnParam.getLevel() > 1 && yarnParam.getParentId() == 0) {
            return OpEntityResult.fail("子队列的父资源ID不能为0");
        }
        result = yarnManagerService.modifyRes(yarnParam);
        return result;
    }

    @PostMapping("/distributeRes")
    @ApiOperation(value = "分配yarn资源，返回数据库里的资源id", notes = "")
    @RolesAllowed({"Tenant"})
    public OpEntityResult<Integer> distributeRes(@ApiParam(value = "yarn传参")
                                                 @RequestBody DistributeAO yarnDistributeAO) throws Exception {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (yarnDistributeAO.getResourceId() == null) {
            return OpEntityResult.fail("资源ID 不可为空");
        }
        result = yarnManagerService.distributeRes(yarnDistributeAO);
        return result;
    }


    @DeleteMapping("/delRes")
    @ApiOperation(value = "删除yarn资源，返回受影响行数", notes = "")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> delRes(@ApiParam(value = "资源ID")
                                             @RequestParam(value = "resourceId") Integer resourceId) {
        if (resourceId == null) {
            return OpEntityResult.fail("资源ID 不可为空");
        }
        OpEntityResult<Integer> result = yarnManagerService.delRes(resourceId);
        return result;
    }

    @GetMapping("/sync")
    @ApiOperation(value = "同步yarn队列信息", notes = "可以看到未重启前的队列信息")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> sync(@ApiParam(value = "资源ID，同步二级队列时才需要。不需要则传0")
                                        @RequestParam(value = "resourceId", required = false) Integer resourceId,
                                          @ApiParam(value = "资源层级")
                                        @RequestParam(value = "level") Integer level) {
        if (level == null) level = 1;//默认同步第一级队列
        if (resourceId == null) resourceId = 0;
        if (level == 2 && (resourceId == null || resourceId == 0)) {
            return OpEntityResult.fail("同步二级队列时必传资源ID");
        }
        return yarnManagerService.sync(resourceId, level);
    }
}
